<template>
  <div class="register-container">
    <div class="register-card">
      <!-- 左侧品牌区域 -->
      <div class="brand-section">
        <div class="brand-content">
          <div class="brand-header">
            <i class="fas fa-graduation-cap"></i>
            <h1>智慧考试系统</h1>
          </div>
          
          <div class="features">
            <div class="feature-item">
              <div class="feature-icon">
                <i class="fas fa-user-plus"></i>
              </div>
              <div class="feature-text">
                <h3>快速注册</h3>
                <p>简单几步即可完成注册，快速开始您的学习之旅</p>
              </div>
            </div>
            
            <div class="feature-item">
              <div class="feature-icon">
                <i class="fas fa-book-open"></i>
              </div>
              <div class="feature-text">
                <h3>丰富题库</h3>
                <p>海量题库资源，涵盖多个学科领域，助力学习提升</p>
              </div>
            </div>
            
            <div class="feature-item">
              <div class="feature-icon">
                <i class="fas fa-chart-line"></i>
              </div>
              <div class="feature-text">
                <h3>学习分析</h3>
                <p>详细的学习报告和成绩分析，帮助您了解学习进度</p>
              </div>
            </div>
          </div>
        </div>
        
        <div class="brand-image">
          <img src="https://design.gemcoder.com/staticResource/echoAiSystemImages/4a59c1a4e78f15392ae23b7f5447f813.png" alt="教育科技插图" />
        </div>
      </div>
      
      <!-- 右侧表单区域 -->
      <div class="form-section">
        <div class="form-container">
          <div class="form-header">
            <h2>学生注册</h2>
            <p>创建您的学习账户</p>
          </div>
          
          <el-form
            ref="registerFormRef"
            :model="registerForm"
            :rules="registerRules"
            @submit.prevent="handleRegister"
            class="register-form"
          >
            <el-form-item prop="name">
              <el-input
                v-model="registerForm.name"
                placeholder="请输入真实姓名"
                size="large"
                prefix-icon="User"
              />
            </el-form-item>
            
            <el-form-item prop="studentId">
              <el-input
                v-model="registerForm.studentId"
                placeholder="请输入学号"
                size="large"
                prefix-icon="Postcard"
              />
            </el-form-item>
            
            <el-form-item prop="email">
              <el-input
                v-model="registerForm.email"
                placeholder="请输入邮箱地址"
                size="large"
                prefix-icon="Message"
              />
            </el-form-item>
            

            
            <el-form-item prop="password">
              <el-input
                v-model="registerForm.password"
                type="password"
                placeholder="请输入密码"
                size="large"
                prefix-icon="Lock"
                show-password
              />
            </el-form-item>
            
            <el-form-item prop="confirmPassword">
              <el-input
                v-model="registerForm.confirmPassword"
                type="password"
                placeholder="请确认密码"
                size="large"
                prefix-icon="Lock"
                show-password
                @keyup.enter="handleRegister"
              />
            </el-form-item>
            
            <el-form-item prop="agreement">
              <el-checkbox v-model="registerForm.agreement">
                我已阅读并同意
                <el-link type="primary" @click="showTerms">《用户协议》</el-link>
                和
                <el-link type="primary" @click="showPrivacy">《隐私政策》</el-link>
              </el-checkbox>
            </el-form-item>
            
            <el-button
              type="primary"
              size="large"
              :loading="loading"
              @click="handleRegister"
              class="register-btn"
            >
              {{ loading ? '注册中...' : '立即注册' }}
            </el-button>
          </el-form>
          
          <div class="form-footer">
            <p>
              已有账户？
              <router-link to="/login" class="login-link">
                立即登录
              </router-link>
            </p>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 用户协议对话框 -->
    <el-dialog
      v-model="termsVisible"
      title="用户协议"
      width="600px"
      class="terms-dialog"
    >
      <div class="terms-content">
        <h3>1. 服务条款</h3>
        <p>欢迎使用智慧考试系统。在使用本系统前，请仔细阅读以下条款。</p>
        
        <h3>2. 用户责任</h3>
        <p>用户应当保证所提供的注册信息真实、准确、完整，并承担因信息不实而产生的一切责任。</p>
        
        <h3>3. 考试规则</h3>
        <p>用户在参加考试时应当遵守考试纪律，不得作弊或协助他人作弊。</p>
        
        <h3>4. 数据安全</h3>
        <p>我们承诺保护用户的个人信息和考试数据安全，不会泄露给第三方。</p>
        
        <h3>5. 服务变更</h3>
        <p>我们保留随时修改或终止服务的权利，并会提前通知用户。</p>
      </div>
      <template #footer>
        <el-button @click="termsVisible = false">关闭</el-button>
      </template>
    </el-dialog>
    
    <!-- 隐私政策对话框 -->
    <el-dialog
      v-model="privacyVisible"
      title="隐私政策"
      width="600px"
      class="privacy-dialog"
    >
      <div class="privacy-content">
        <h3>1. 信息收集</h3>
        <p>我们收集您提供的注册信息，包括姓名、学号、邮箱等。</p>
        
        <h3>2. 信息使用</h3>
        <p>收集的信息仅用于提供考试服务、身份验证和系统管理。</p>
        
        <h3>3. 信息保护</h3>
        <p>我们采用先进的加密技术和安全措施保护您的个人信息。</p>
        
        <h3>4. 信息共享</h3>
        <p>除法律要求外，我们不会与第三方共享您的个人信息。</p>
        
        <h3>5. 用户权利</h3>
        <p>您有权查看、修改或删除您的个人信息。</p>
      </div>
      <template #footer>
        <el-button @click="privacyVisible = false">关闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { register } from '@/api/auth/login'

const router = useRouter()
const registerFormRef = ref()
const loading = ref(false)
const termsVisible = ref(false)
const privacyVisible = ref(false)

const registerForm = reactive({
  name: '',
  studentId: '',
  email: '',

  password: '',
  confirmPassword: '',
  agreement: false
})

// 自定义验证规则
const validateConfirmPassword = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请确认密码'))
  } else if (value !== registerForm.password) {
    callback(new Error('两次输入的密码不一致'))
  } else {
    callback()
  }
}

const validateAgreement = (rule, value, callback) => {
  if (!value) {
    callback(new Error('请阅读并同意用户协议和隐私政策'))
  } else {
    callback()
  }
}

const registerRules = {
  name: [
    { required: true, message: '请输入真实姓名', trigger: 'blur' },
    { min: 2, max: 20, message: '姓名长度在 2 到 20 个字符', trigger: 'blur' }
  ],
  studentId: [
    { required: true, message: '请输入学号', trigger: 'blur' },
    { pattern: /^[0-9]{8,12}$/, message: '学号应为8-12位数字', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱地址', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
  ],

  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' },
    { pattern: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d@$!%*?&]{6,}$/, message: '密码必须包含大小写字母和数字', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, validator: validateConfirmPassword, trigger: 'blur' }
  ],
  agreement: [
    { required: true, validator: validateAgreement, trigger: 'change' }
  ]
}

const handleRegister = async () => {
  if (!registerFormRef.value) return
  
  try {
    await registerFormRef.value.validate()
    loading.value = true
    
    // 调用注册API
    const result = await register({
      username: registerForm.studentId, // 使用学号作为用户名
      password: registerForm.password,
      email: registerForm.email,
      role: 'student',
      realname: registerForm.name // 添加真实姓名
    })
    
    ElMessage.success('注册成功！请登录您的账户')
    
    // 跳转到登录页面
    router.push('/login')
    
  } catch (error) {
    console.error('注册失败:', error)
    ElMessage.error(error.message || '注册失败，请重试')
  } finally {
    loading.value = false
  }
}

const showTerms = () => {
  termsVisible.value = true
}

const showPrivacy = () => {
  privacyVisible.value = true
}
</script>

<style scoped>
.register-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.register-card {
  background: white;
  border-radius: 20px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  display: grid;
  grid-template-columns: 1fr 1fr;
  max-width: 1000px;
  width: 100%;
  min-height: 600px;
}

.brand-section {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 40px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  position: relative;
  overflow: hidden;
}

.brand-section::before {
  content: '';
  position: absolute;
  top: -50%;
  right: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle, rgba(255, 255, 255, 0.1) 0%, transparent 70%);
  animation: float 6s ease-in-out infinite;
}

@keyframes float {
  0%, 100% {
    transform: translateY(0px) rotate(0deg);
  }
  50% {
    transform: translateY(-20px) rotate(180deg);
  }
}

.brand-header {
  text-align: center;
  margin-bottom: 40px;
}

.brand-header i {
  font-size: 48px;
  margin-bottom: 16px;
  display: block;
}

.brand-header h1 {
  font-size: 28px;
  font-weight: 700;
  margin: 0;
  letter-spacing: 1px;
}

.features {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.feature-item {
  display: flex;
  align-items: flex-start;
  gap: 16px;
  padding: 16px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 12px;
  backdrop-filter: blur(10px);
  transition: transform 0.3s ease;
}

.feature-item:hover {
  transform: translateX(8px);
}

.feature-icon {
  width: 40px;
  height: 40px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.feature-icon i {
  font-size: 18px;
}

.feature-text h3 {
  font-size: 16px;
  font-weight: 600;
  margin: 0 0 4px 0;
}

.feature-text p {
  font-size: 14px;
  margin: 0;
  opacity: 0.9;
  line-height: 1.4;
}

.brand-image {
  text-align: center;
  margin-top: 20px;
}

.brand-image img {
  max-width: 200px;
  height: auto;
  opacity: 0.8;
  filter: brightness(1.2);
}

.form-section {
  padding: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.form-container {
  width: 100%;
  max-width: 400px;
}

.form-header {
  text-align: center;
  margin-bottom: 32px;
}

.form-header h2 {
  font-size: 28px;
  font-weight: 700;
  color: #1a202c;
  margin: 0 0 8px 0;
}

.form-header p {
  color: #718096;
  margin: 0;
  font-size: 16px;
}

.register-form {
  margin-bottom: 24px;
}

.register-form .el-form-item {
  margin-bottom: 20px;
}

.register-form .el-input {
  border-radius: 8px;
}

.register-form .el-input__wrapper {
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  border: 1px solid #e2e8f0;
  transition: all 0.3s ease;
}

.register-form .el-input__wrapper:hover {
  border-color: #667eea;
}

.register-form .el-input.is-focus .el-input__wrapper {
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.register-btn {
  width: 100%;
  height: 48px;
  font-size: 16px;
  font-weight: 600;
  border-radius: 8px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  transition: all 0.3s ease;
}

.register-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(102, 126, 234, 0.3);
}

.form-footer {
  text-align: center;
  padding-top: 20px;
  border-top: 1px solid #e2e8f0;
}

.form-footer p {
  color: #718096;
  margin: 0;
}

.login-link {
  color: #667eea;
  text-decoration: none;
  font-weight: 600;
  transition: color 0.3s ease;
}

.login-link:hover {
  color: #764ba2;
}

.terms-dialog .terms-content,
.privacy-dialog .privacy-content {
  max-height: 400px;
  overflow-y: auto;
  padding: 16px;
  line-height: 1.6;
}

.terms-content h3,
.privacy-content h3 {
  color: #1a202c;
  font-size: 16px;
  font-weight: 600;
  margin: 16px 0 8px 0;
}

.terms-content p,
.privacy-content p {
  color: #4a5568;
  margin: 0 0 12px 0;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .register-container {
    padding: 10px;
  }
  
  .register-card {
    grid-template-columns: 1fr;
    max-width: 400px;
  }
  
  .brand-section {
    padding: 30px 20px;
    min-height: 300px;
  }
  
  .brand-header h1 {
    font-size: 24px;
  }
  
  .features {
    gap: 16px;
  }
  
  .feature-item {
    padding: 12px;
  }
  
  .feature-text h3 {
    font-size: 14px;
  }
  
  .feature-text p {
    font-size: 12px;
  }
  
  .brand-image {
    margin-top: 16px;
  }
  
  .brand-image img {
    max-width: 150px;
  }
  
  .form-section {
    padding: 30px 20px;
  }
  
  .form-header h2 {
    font-size: 24px;
  }
}

@media (max-width: 480px) {
  .register-container {
    padding: 5px;
  }
  
  .brand-section {
    padding: 20px 15px;
  }
  
  .form-section {
    padding: 20px 15px;
  }
  
  .form-header h2 {
    font-size: 22px;
  }
}
</style>